package com.hra.autoreset;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView timeMsg;
    private Button startTestButton;
    private Button stopTestButton;

    Handler handler;

    private boolean startCountDown = true;
    /**
     * 恢复出厂设置的间隔
     */
    private int resetIntervalTime = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        handler = new Handler(getMainLooper());

        timeMsg = findViewById(R.id.tv_time);
        startTestButton = findViewById(R.id.startTestButton);
        stopTestButton = findViewById(R.id.stopTestButton);

        startTestButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetIntervalTime = 10;
                startCountDown = true;
                startTestButton.setEnabled(false);
                stopTestButton.setEnabled(startCountDown);
                String msg = "系统将在 " + resetIntervalTime + " 秒后自动恢复出厂设置";
                timeMsg.setText(msg);
                handler.postDelayed(runnable, 1000);
            }
        });
        stopTestButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startCountDown = false;
                startTestButton.setEnabled(true);
                stopTestButton.setEnabled(startCountDown);
                handler.removeCallbacksAndMessages(null);
                String msg = "自动恢复出厂设置循环已停止";
                timeMsg.setText(msg);
            }
        });

        startTestButton.setEnabled(!startCountDown);
        stopTestButton.setEnabled(startCountDown);
        if (startCountDown) {
            String msg = "系统将在 " + resetIntervalTime + " 秒后自动恢复出厂设置";
            timeMsg.setText(msg);
            handler.postDelayed(runnable, 1000);
        }
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            updateTime();
            if (startCountDown) {
                // 每隔1秒执行一次
                handler.postDelayed(this, 1000);
            }
        }
    };

    private void updateTime() {
        resetIntervalTime -= 1;
        if (resetIntervalTime >= 0) {
            String msg = "系统将在 " + resetIntervalTime + " 秒后自动恢复出厂设置";
            timeMsg.setText(msg);
            LogUtil.logD(this, msg);
        } else {
            startCountDown = false;
            Intent mIntent = new Intent("android.intent.action.FACTORY_RESET");
            mIntent.setPackage("android");
            mIntent.putExtra("android.intent.extra.REASON", "MasterClearConfirm");
            sendBroadcast(mIntent);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (!startCountDown) {
            return super.onKeyDown(keyCode, event);
        }
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new AlertDialog.Builder(this)
                    .setTitle("是否确定中止恢复出厂设置测试？")
                    .setPositiveButton("确认退出", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            startCountDown = false;
                            startTestButton.setEnabled(true);
                            stopTestButton.setEnabled(startCountDown);
                            handler.removeCallbacksAndMessages(null);
                            String msg = "自动恢复出厂设置已中止";
                            timeMsg.setText(msg);
                        }
                    })
                    .setNegativeButton("取消", null)
                    .show();
            return true;
        }
        if (keyCode == KeyEvent.KEYCODE_HOME || keyCode == KeyEvent.KEYCODE_MENU) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
